<div class="layui-fluid layui-anim febs-anim" id="febs-user" lay-title="客户管理">
    <div class="layui-row febs-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" lay-filter="user-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md10">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">昵称</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="nickname" autocomplete="off"
                                                   placeholder="输入用户昵称进行检索" class="layui-input">
                                        </div>
                                    </div>

                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">真实姓名</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="trueName" autocomplete="off"
                                                   placeholder="输入用户姓名进行检索" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">性别</label>
                                        <div class="layui-input-inline">
                                            <select name="sex">
                                                <option value="">请选择性别</option>
                                                <option value="1">男性</option>
                                                <option value="2">女性</option>
                                                <option value="0">保密</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="query">
                                    <i class="layui-icon">&#xe848;</i>搜索
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
                                    <i class="layui-icon">&#xe79b;</i>刷新
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                                     shiro:hasAnyPermissions="user:add,user:update,user:password:reset,user:export">
                                    <i class="layui-icon">&#xe875;</i>更多
                                </div>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="userTable" lay-data="{id: 'userTable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="user-status">
    {{#
    var status = {
    1: {title: '有效', color: 'green'},
    0: {title: '禁用', color: 'red'}
    }[d.status];
    }}
    <span class="layui-badge febs-tag-{{status.color}}">{{ status.title }}</span>
</script>
<script type="text/html" id="user-sex">
    {{#
    var sex = {
    2: {title: '保密'},
    1: {title: '女'},
    0: {title: '男'}
    }[d.sex];
    }}
    <span>{{ sex.title }}</span>
</script>
<script type="text/html" id="customer-option">
    <span shiro:lacksPermission="user:view,user:update,user:delete">
        <span class="layui-badge-dot febs-bg-orange"></span> 无权限
    </span>
    <a lay-event="detail" shiro:hasPermission="customer:view"><i
            class="layui-icon febs-edit-area febs-green">&#xe7a5;</i><span class="febs-green">查看</span></a>
    <a lay-event="incomeList" shiro:hasPermission="income:view"><i
            class="layui-icon febs-edit-area febs-orange">&#xe80c;</i><span class="febs-">收入详情</span></a>
    <a lay-event="incomeEdit" shiro:hasPermission="income:update"><i
            class="layui-icon febs-edit-area febs-red">&#xe7a4;</i><span class="febs-red">提现</span></a>
<!--    <a lay-event="del" shiro:hasPermission="customer:delete"><i class="layui-icon febs-edit-area febs-red">&#xe640;</i><span-->
<!--            class="febs-red">删除</span></a>-->
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdown', 'jquery', 'upload', 'laydate', 'form', 'formSelects', 'table', 'febs'], function () {
        var $ = layui.jquery,
            febs = layui.febs,
            form = layui.form,
            table = layui.table,
            dropdown = layui.dropdown,
            $view = $('#febs-user'),
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'createTime', type: null},
            tableIns,
            createTimeFrom,
            createTimeTo;

        form.render();

        initTable();

        // dropdown.render({
        //     elem: $view.find('.action-more'),
        //     click: function (name, elem, event) {
        //         var checkStatus = table.checkStatus('userTable');
        //         if (name === 'delete') {
        //             if (!checkStatus.data.length) {
        //                 febs.alert.warn('请选择需要删除的客户');
        //             } else {
        //                 febs.modal.confirm('删除客户', '确定删除该客户？', function () {
        //                     var customerIds = [];
        //                     layui.each(checkStatus.data, function (key, item) {
        //                         customerIds.push(item.customerId)
        //                     });
        //                     deleteUsers(customerIds.join(','));
        //                 });
        //             }
        //         }
        //     },
        //     options: [ {
        //         name: 'delete',
        //         title: '删除客户',
        //         perms: 'user:delete'
        //     }]
        // });


        table.on('tool(userTable)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            console.log("data",data)
            if (layEvent === 'detail') {
                febs.modal.view('客户信息', 'system/customer/customerView/' + data.nickname, {
                    area: $(window).width() <= 750 ? '95%' : '660px'
                });
            }
            if (layEvent === 'incomeList') {
                febs.modal.view('收入信息', 'system/income/incomeList/' + data.customerId, {
                    // area: $(window).width() <= 750 ? '95%' : '800px'
                    area:['1000px','800px'],
                    end:function () {
                        $reset.click();
                    }
                });
                // febs.modal.open('收入信息', 'system/income/incomeList/' + data.customerId, {
                //     area: ['50%', '70%'],
                //     btn: ['提交', '取消'],
                //     // yes: function (index, layero) {
                //     //     $('#content-update-form').find('#submit').trigger('click');
                //     // },
                //     btn2: function () {
                //         layer.closeAll();
                //     }
                // });
            }
            if (layEvent === 'del') {
                febs.modal.confirm('删除客户', '确定删除该客户？', function () {
                    deleteUsers(data.customerId);
                });
            }

            if (layEvent === 'incomeEdit') {
                febs.modal.open('钱包提现', 'system/customer/withdraw/' + data.customerId, {
                    area: ['50%', '70%'],
                    btn: ['提交', '取消'],
                    yes: function (index, layero) {
                        $('#income-update-form').find('#submit').trigger('click');
                    },
                    btn2: function () {
                        layer.closeAll();
                    }
                });
            }
            if (layEvent === 'toSeeDetails') {
                var index = layer.open({
                    title: "查看图片详情",
                    content: data.headimgurl,
                    type: 2,
                    maxmin: true,
                    area: ['50%','50%'],
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        // console.log(body.html()); //得到iframe页的body内容
                        // body.find('#goods_id').val(ids);
                    }
                });
            }
        });

        table.on('sort(userTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        $query.on('click', function () {
            console.log(sortObject.field);
            var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            console.log(params.roleId);
            tableIns.reload({where: params, page: {curr: 1}});
        });

        $reset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            createTimeTo = null;
            createTimeFrom = null;
            tableIns.reload({where: getQueryParams(),  initSort: sortObject,
                done: function(res, curr, count){
                    //如果是异步请求数据方式，res即为你接口返回的信息。
                    //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                    console.log(res);
                    //得到当前页码
                    console.log(curr);
                    //得到数据总量
                    console.log(count);
                    var beforeCurr = curr;
                    //当前页数据的个数
                    var dataLength = res.data.length;
                    var count = res.count;
                    //如果当前页数据的个数等于0，并且总数不为0，则跳转到上一页
                    if( dataLength == 0 && count!= 0 ){
                        table.reload("userTable",{
                            page:{curr:beforeCurr - 1}
                        })
                    }
                }
            });
        });

        function initTable() {
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'userTable',
                url: ctx + 'customer/getCustomerList',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'nickname', title: '昵称', minWidth: 100},
                    {field:'trueName',title:'真实姓名'},
                    { title: '手机',templet: function (d) {
                            if (d.mobile!=null){
                                var str = d.mobile;
                                var str2 = str.substr(0,3)+"****"+str.substr(7);
                                return str2;
                            }else {
                                return '';
                            }
                        }},
                    { title: '性别', templet: function (d) {
                            if(d.sex==1){
                                return '<span class="layui-badge febs-tag-green">男性</span>';
                            }else if(d.sex==2){
                                return '<span class="layui-badge febs-tag-volcano">女性</span>';
                            }else {
                                return '<span class="layui-badge febs-tag-volcano">未知</span>'
                            }
                        }},
                    {field: 'headimgurl', title: '头像', align:'center', templet: function (d) {
                            if (d.headimgurl!=null) {
                                return '<img style="width: 30px" lay-event="toSeeDetails" src="' + d.headimgurl + '">'
                            }else {
                                return '';
                            }
                        }},
                    {field:'province',title: '省份'},
                    {field:'city',title: '城市'},
                    {field:'country',title: '国家'},
                    {field:'wallet',title:'钱包余额',templet:function (d) {
                            if(d.wallet!=null){
                                return d.wallet
                            }else{
                                return '0.00';
                            }
                        }},
                    {title: '状态', templet: function (d) {
                            if(d.status==1){
                                return '<span class="layui-badge febs-tag-green">有效</span>';
                            }else{
                                return '<span class="layui-badge febs-tag-volcano">锁定</span>';
                            }
                        }},
                    {title: '操作', toolbar: '#customer-option', minWidth: 220}
                ]]
            });
        }

        function getQueryParams() {
            var createTime = $searchForm.find('input[name="createTime"]').val();
            if (createTime) {
                createTimeFrom = createTime.split(' - ')[0];
                createTimeTo = createTime.split(' - ')[1];
            }
            return {
                trueName: $searchForm.find('input[name="trueName"]').val(),
                sex: $searchForm.find("select[name='sex']").val(),
                nickname: $searchForm.find("select[name='nickname']").val(),
                invalidate_ie_cache: new Date()
            };
        }

        function deleteUsers(customerIds) {
            febs.get(ctx + 'customer/delete/' + customerIds, null, function () {
                febs.alert.success('删除客户成功');
                $reset.click();
            });
        }
    })
</script>